package com.cust.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cust.sky.context.BaseContext;
import com.cust.sky.dto.ShoppingCartDTO;
import com.cust.sky.entity.Dish;
import com.cust.sky.entity.DishFlavor;
import com.cust.sky.entity.Setmeal;
import com.cust.sky.entity.ShoppingCart;
import com.cust.sky.mapper.ShoppingCartMapper;
import com.cust.sky.service.DishFlavorService;
import com.cust.sky.service.DishService;
import com.cust.sky.service.SetmealService;
import com.cust.sky.service.ShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cust.sky.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author lei
 * @since 2024-07-10
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前加入到购物车中的商品是否已经存在了
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        List<ShoppingCart> shoppingCartList = getShoppingCartList(shoppingCart);
        //如果已经存在了，只需要将数量加一
        if (!CollectionUtils.isEmpty(shoppingCartList)) {
            ShoppingCart cart = shoppingCartList.get(0);
            cart.setNumber(cart.getNumber() + 1);
            baseMapper.updateById(cart);
        } else {
            //如果不存在，需要插入一条购物车数据
            //判断本次添加到购物车的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //本次添加到购物车的是菜品
                Dish dish = dishService.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //本次添加到购物车的是套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealService.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            baseMapper.insert(shoppingCart);
        }
    }

    /**
     * 查询购物车列表
     *
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        //获取到当前微信用户的id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = baseMapper.selectOne(new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId, userId));
        if (shoppingCart == null) {
            return null;
        }
        List<ShoppingCart> shoppingCartList = getShoppingCartList(shoppingCart);
        return shoppingCartList;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        //获取到当前微信用户的id
        Long userId = BaseContext.getCurrentId();
        baseMapper.delete(new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId, userId));
    }

    /**
     * 删除购物车中一个商品
     *
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获取到当前微信用户的购物车数据
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart1 = baseMapper.selectOne(new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId, userId));
        List<ShoppingCart> shoppingCartList = getShoppingCartList(shoppingCart1);

        if (!CollectionUtils.isEmpty(shoppingCartList)) {
            shoppingCart = shoppingCartList.get(0);

            Integer number = shoppingCart.getNumber();
            if (number == 1) {
                //当前商品在购物车中的份数为1，直接删除当前记录
                baseMapper.deleteById(shoppingCart.getId());
            } else {
                //当前商品在购物车中的份数不为1，修改份数即可
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                baseMapper.updateById(shoppingCart);
            }
        }
    }

    /**
     * 查询当前用户的购物车数据
     *
     * @param userId
     * @return
     */
    @Override
    public List<ShoppingCart> getShoppingCartByUserId(Long userId) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> shoppingCartList = baseMapper.selectList(wrapper);
        return shoppingCartList;
    }

    /**
     * 清空当前用户的购物车数据
     *
     * @param userId
     */
    @Override
    public void deleteByUserId(Long userId) {
        baseMapper.delete(new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId, userId));
    }

    /**
     * 条件查询购物车列表
     *
     * @param shoppingCart
     * @return
     */
    private List<ShoppingCart> getShoppingCartList(ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        Long userId = shoppingCart.getUserId();
        Long setmealId = shoppingCart.getSetmealId();
        Long dishId = shoppingCart.getDishId();
        String dishFlavor = shoppingCart.getDishFlavor();
        if (userId != null) {
            wrapper.eq(ShoppingCart::getUserId, userId);
        }
        if (setmealId != null) {
            wrapper.eq(ShoppingCart::getSetmealId, setmealId);
        }
        if (dishId != null) {
            wrapper.eq(ShoppingCart::getDishId, dishId);
        }
        if (StringUtils.hasText(dishFlavor)) {
            wrapper.eq(ShoppingCart::getDishFlavor, dishFlavor);
        }
        return baseMapper.selectList(wrapper);
    }
}
